package com.itheima.Task;

import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Set;

/**
 * @author 32338
 * @date 2024-05-04 12:14:17
 * @description 定时处理阿里云上传的图片
 */
@Component
@Slf4j
public class CleanImgTask {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Scheduled(cron = "0 0 1 * * ?")
    public void clean(){
        log.info("执行清理任务，每天自动清理一次");
        //比对两个集合中的差值
        Set differenceImg = redisTemplate.opsForSet().difference("image", "userImage");
        if(differenceImg!=null&&differenceImg.size()>0){
            for (Object objectName: differenceImg) {
                aliOssUtil.delete((String) objectName);
            }
        }


    }
}

